<template>
  <view class="goods-list">
    <view class="goods-list-item" v-for="(item,index) in goodsList" :key="item.goods_id" @click="goDetail(item.goods_id)">
      <goods :goods=item></goods>
    </view>
  </view>
</template>

<script>
  import toast from '@/utils/toast.js'
  import { getSearchGoods } from "@/api/goods.js"
  export default {
    data() {
      return {
        queryObj:{
          query:'',//关键词
          cid:'',//商品分类id
          pagenum:1,//当前页
          pagesize:10//每页数据条数
        },
        goodsList:[],
        total:0,
        isLoading:false
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      this.getGoods()
    },
    
    //上拉加载更多
    onReachBottom() {
      const {total, queryObj:{pagenum ,pagesize}} = this
      if ((pagenum-1) * pagesize >= total) return toast("没有更多数据啦~",'none')
      if (this.isLoading) return
      this.getGoods()
    },
    
    //下拉刷新
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.goodsList = []
      this.total = 0
      this.getGoods(() => uni.stopPullDownRefresh())
    },
    methods:{
      async getGoods(stopRefresh) {
        uni.showLoading({
          title:'加载中...'
        })
        this.isLoading = true
        try {
          const res = await getSearchGoods(this.queryObj)
          console.log(res);
          this.goodsList = [...this.goodsList,...res.message.goods]
          this.total = res.message.total
          this.queryObj.pagenum += 1
        } finally {
          stopRefresh && stopRefresh()
          this.isLoading = false
          uni.hideLoading()
        }
      },
    goDetail(id) {
      console.log(id)
      uni.navigateTo({
        url:'/subpkg/goods_detail/goods_detail?goods_id=' + id
      })
    }
    
    },
  }
</script>

<style lang="scss">

</style>
